package com.qsboss.demo.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qsboss.demo.domain.Result;
import com.qsboss.demo.mapper.TbBillMapper;
import com.qsboss.demo.mapper.TbRepaymentMapper;
import com.qsboss.demo.pojo.TbBill;
import com.qsboss.demo.pojo.TbRepayment;
import com.qsboss.demo.service.TbBillService;
import com.qsboss.demo.vo.CustomerNeedPay;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author 军哥
 * @version 1.0
 * @description: 账单服务层
 * @date 2023/10/25 10:37
 */

@Service
@Slf4j
public class TbBillServiceImpl implements TbBillService {

    @Autowired
    TbBillMapper tbBillMapper;

    @Autowired
    TbRepaymentMapper tbRepaymentMapper;

    @Autowired
    RuntimeService runtimeService;

    @Override
    public PageInfo getBillByPage(int curPage, int pageSize) {

        PageHelper.startPage(curPage, pageSize);

        Page<TbBill> billByPage = tbBillMapper.getBillByPage();

        return new PageInfo(billByPage);
    }

    @Override
    public PageInfo getBillInCustomerByPage(int curPage, int pageSize, Integer customerId) {
        PageHelper.startPage(curPage, pageSize);

        Page<TbBill> billByPage = tbBillMapper.getBillInCustomerByPage(customerId);

        return new PageInfo(billByPage);
    }

    @Override
    public Result submitClose(Integer billId) {

        //-- 获取账单信息
        TbBill tbBill = tbBillMapper.getBillById(billId);
        if(tbBill == null) {
            return Result.FAILED(404, "账单不存在");
        }

        //-- 检查还款记录
        TbRepayment repaymentByBiilId = tbRepaymentMapper.getRepaymentByBiilId(tbBill.getBillId());
        if(tbBill == null) {
            return Result.FAILED(404, "没有还款记录");
        }

        //-- 启动销账流程
        String businessKey = "" + tbBill.getBillId();
        String key = "closebill";
        HashMap<String, Object> map = new HashMap<>();

        if(key.equals("closebill")) {
            // 结账流程
            map.put("user1", "代志伟");
            map.put("user2", "孙伟杰");
            map.put("optCode", 1);
        }

        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(key, businessKey, map);
        if(processInstance != null) {
            log.info("processId:" + processInstance.getId());
            return Result.SUCCESS();
        }

        //tbBillMapper.setBillPayAck(billId, 2);


        return Result.SUCCESS();
    }

    @Override
    public TbBill getBillById(Integer billId) {

        TbBill tbBill = tbBillMapper.getBillById(billId);

        return tbBill;
    }

    @Override
    public Result setAckMail(Integer billId, int i) {
        tbBillMapper.setAckMail(billId, i);
        return Result.SUCCESS();
    }

    @Override
    public Result setMailSend(Integer billId, int flag) {
        tbBillMapper.setMailSend(billId, flag);
        return Result.SUCCESS();
    }

    @Override
    public TbBill randomBill() {

        TbBill tbBill = tbBillMapper.getRandomBill();

        return tbBill;
    }

    @Override
    public void addBill(TbBill tbBill) {
        tbBillMapper.addBill(tbBill);
    }

    @Override
    public Result getCustomerNeedPay() {

        List<CustomerNeedPay> customerNeedPay = tbBillMapper.getCustomerNeedPay();

        // 柱状图和折线图数据
        ArrayList<String> names = new ArrayList<>();
        ArrayList<Double> amounts = new ArrayList<>();
        // 饼状图的数据
        ArrayList<HashMap<String, Object>> hashMapArrayList = new ArrayList<>();

        for (CustomerNeedPay needPay : customerNeedPay) {
            names.add(needPay.getCustomerName());
            amounts.add(needPay.getAmount());

            // 生成饼状图数据
            HashMap<String, Object> integerHashMap = new HashMap<>();
            integerHashMap.put("value", needPay.getAmount());
            integerHashMap.put("name", needPay.getCustomerName());

            hashMapArrayList.add(integerHashMap);
        }

        // 返回数据
        HashMap<String, Object> map = new HashMap<>();
        map.put("names", names);
        map.put("amounts", amounts);
        map.put("pie", hashMapArrayList);

        return Result.SUCCESS(map);
    }

    @Override
    public HSSFWorkbook exportBill(String billDate) throws IOException {
        //--1 从数据库获取数据
        List<TbBill> bills = tbBillMapper.getBillListByDate(billDate);

        //--2 创建excel文件
        HSSFWorkbook workbook = new HSSFWorkbook();
        //-- 创建工作簿
        HSSFSheet sheet = workbook.createSheet("客户账单");

        //-- 创建表头
        HSSFRow TitleRow = sheet.createRow(0);

        CellRangeAddress cellAddresses = new CellRangeAddress(0, 0, 0, 4);
        sheet.addMergedRegionUnsafe(cellAddresses);

        // 设置表头的内容
        HSSFCell cell = TitleRow.createCell(0);
        cell.setCellValue("客户账单表 " + billDate);

        //-- 设置水平居中
        HSSFCellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setAlignment(HorizontalAlignment.CENTER);

        // -- 设置单元格样式
        cellStyle.setFillBackgroundColor(IndexedColors.RED.index);

        HSSFFont fontT = workbook.createFont();
        fontT.setBold(true);
        fontT.setFontName("黑体");
        fontT.setFontHeightInPoints((short) 14);
        cellStyle.setFont(fontT);

        // 应用单元格样式
        cell.setCellStyle(cellStyle);



        //--3 表头:第二行
        HSSFRow conlumRow = sheet.createRow(1);

        HSSFCell cell0 = conlumRow.createCell(0);
        cell0.setCellValue("账单ID");

        HSSFCell cell1 = conlumRow.createCell(1);
        cell1.setCellValue("账单说明");

        HSSFCell cell2 = conlumRow.createCell(2);
        cell2.setCellValue("账单金额");

        HSSFCell cell3 = conlumRow.createCell(3);
        cell3.setCellValue("账单日期");

        HSSFCell cell4 = conlumRow.createCell(4);
        cell3.setCellValue("客户姓名");

        //--4 表体：
        int index = 0;
        for (TbBill bill : bills) {
            HSSFRow row = sheet.createRow(index+2);

            HSSFCell cell00 = row.createCell(0);
            cell00.setCellValue(bill.getBillId());

            HSSFCell cell11 = row.createCell(1);
            cell11.setCellValue(bill.getBillName());

            HSSFCell cell22 = row.createCell(2);
            cell22.setCellValue(bill.getBillAmount());

            // 大于50，设置为红色
            if(bill.getBillAmount() > 50) {
                HSSFFont font = workbook.createFont();
                font.setColor(IndexedColors.RED.index);
                HSSFCellStyle style = workbook.createCellStyle();
                style.setFont(font);
                cell22.setCellStyle(style);
            }

            HSSFCell cell33 = row.createCell(3);
            cell33.setCellValue(bill.getCreateTime());

            HSSFCell cell44 = row.createCell(4);
            cell44.setCellValue(bill.getCustomerName());

            index++;
        }

        return workbook;
    }

    @Override
    public int setBillPayAck(Integer billId, Integer ack) {

        tbBillMapper.setBillPayAck(billId, ack);
        return 1;
    }
}
